<template>
  <div>
    <!--搜索查询-->
    <el-row>
      <el-col :span="3">&nbsp;</el-col>
      <el-col :span="3"
        >
        <el-select v-model="searchForm.submodule" size="mini" placeholder="请选择所属模块">
          <el-option  value="服务规则"></el-option>
          <el-option  value="常见问题"></el-option>
          <el-option  value="新手上路"></el-option>
        </el-select>
      </el-col>
      <el-col :span="3"
        ><el-input
          v-model="searchForm.question"
          size="mini"
          clearable
          placeholder="请输入问题"
        ></el-input
      ></el-col>
      <el-col :span="3"
        ><el-button
          type="primary"
          size="mini"
          @click="search()"
          icon="el-icon-search"
          >搜索</el-button
        ></el-col
      >
      <el-col :span="3"
        ><el-button
          type="primary"
          size="mini"
          @click="handlerAdd()"
          icon="el-icon-circle-plus"
          >添加</el-button
        ></el-col
      >
      <el-col :span="3"
        ><el-button
          type="primary"
          size="mini"
          @click="flush()"
          icon="el-icon-refresh"
          >刷新</el-button
        ></el-col
      >
    </el-row>

    <el-row>
      <el-table :data="tableData" style="width: 100%">
        <el-table-column prop="submodule" label="所属模块" width="150">
        </el-table-column>
      
        <el-table-column
          prop="question"
          align="center"
          label="问题"
          width="180"
        >
        </el-table-column>

        <el-table-column prop="answer" label="答案" align="center" width="380">
        </el-table-column>

        <!-- <el-table-column prop="delFlag" :formatter="delFormat" label="删除状态">
        </el-table-column> -->

        <el-table-column label="操作" fixed="right" width="250" align="center">
          <template slot-scope="scope">
            <el-button
              size="mini"
              @click="handleEdit(scope.$index, scope.row)"
              >编辑</el-button
            >
            <el-button
              size="mini"
              type="danger"
              @click="handleDelete(scope.$index, scope.row)"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>
    </el-row>
    <el-row>
      <!--分页插件-->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="currentPage"
        :page-sizes="[2, 3, 5, 10, 20]"
        :page-size="pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      >
      </el-pagination>
    </el-row>

    <!--添加/更新对话框-->
    <el-dialog :title="title" :visible.sync="dialogFormVisible">
      <el-form :model="form" ref="form">
        <el-form-item
          label="所属模块"
          prop="submodule"
          :label-width="formLabelWidth"
        >
          <el-select v-model="form.submodule" placeholder="请选择所属模块">
            <el-option label="服务规则"  value="服务规则"></el-option>
            <el-option label="常见问题"  value="常见问题"></el-option>
            <el-option label="新手上路"  value="新手上路"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item
          label="问题"
          prop="question"
          :label-width="formLabelWidth"
        >
          <el-input v-model="form.question" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="答案" prop="answer" :label-width="formLabelWidth">
          <el-input v-model="form.answer" autocomplete="off"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="submitForm1()">取 消</el-button>
        <el-button type="primary" @click="submitForm('form')">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
export default {
  data() {
    return {
      value: true,
      tableData: [],
      currentPage: 1, //页码 第几页
      pageSize: 5, //每页显示数量
      total: 100, //总条数
      searchForm: { submodule:"",question:""}, //搜索form表单
      title: "问题添加",
      dialogFormVisible: false, //是否显示对话框  默认是不显示
      form: {}, //form表单对象
      formLabelWidth: "120px",
      submoduleList:[]
    };
  },
  created() {
    //初始化方法 类似于jquery $({})
    this.loadData();
    this.loadSubmodule();
  },
  //所有函数
  methods: {
    //加载问题模块的下拉框
    async loadSubmodule(){
      let {data} = await this.$http.post("/marketing/api/customerIssues/findAll", {
        pageNo: 1,
        pageSize: 100,
        data:{dictType:"mks_module_type"}
      });
      console.log(data);
      this.submoduleList=data.data;
    },
    //加载数据
    loadData(){
      this.$http.post("/marketing/api/customerIssues/select?currentPage="+this.currentPage+"&pageSize="+this.pageSize,this.searchForm).then(resp=>{
        console.log(resp)
        if(resp.data.code==2000){
          this.tableData=resp.data.data.list;
          this.total=resp.data.data.total;
        }else if (resp.data.code==4001){
          this.$message.error("未登录");
          this.$router.push("/login");
        }

      })
    },
    //处理更新
    handleEdit(index, row) {
      var _this = this;
      //row  当前行数据 如果是第一行  row = {deptId:1,deptName:'管理部'把一行数据赋值给form赋值,description:'店长的事',status:1}
      //console.log(index, row);
      //显示对话框
      _this.dialogFormVisible = true;
      //指定对话框标题
      _this.title = "问题更新";
      //把一行数据赋值给form赋值
      _this.form = row;
    },
    //处理添加
    handlerAdd() {
      //显示对话框
      this.dialogFormVisible = true;
      //指定对话框标题
      this.title = "问题添加";
      //让form表单重新赋空值
      this.form = {};
    },
    //提交表单
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          var url = "";
          if (this.title == "问题添加") {
            url = "/marketing/api/customerIssues/insert";
          } else {
            url = "/marketing/api/customerIssues/update";
          }
          //让_this等于当前vue对象
          var _this = this;
          // alert(111);
          //赋值 $http官网代码  this.form =form:{deptName:'',description:'',status:1}
          _this.$http
            .post(url, this.form) //指定请求的url及参数  get
            .then(function (response) {
              //正确返回对象的处理
              // handle success
              //console.log(response);
              //JSON.stringify   把json转换为json字符串，方便查看
              //console.log(JSON.stringify(response));
              //直接使用this 会指 $http对象  错误
              // this.tableData
              //判断返回值是否正确
              if (response.data.code == 2000) {
                //关闭弹框
                _this.dialogFormVisible = false;
                //提示添加成功
                _this.$message({
                  message: "恭喜你，这是一条成功消息",
                  type: "success",
                });
                //重新加载数据
                _this.loadData();
              } else {
                //错误提示信息
                _this.$message.error("错了哦，这是一条错误消息");
              }
            })
            .catch(function (error) {
              //错误的处理
              // handle error
              //console.log(error);
            });
        } else {
          //console.log("error submit!!");
          return false;
        }
      });
    },
    zhFormat(row, column, cellValue, index) {
      //row当前行数据
      //column 列名称
      //cellValue 列的值
      //index 下标
      //alert(cellValue);
      return cellValue == 0 ? "正常" : "异常";
    },
    delFormat(row, column, cellValue, index) {
      //row当前行数据
      //column 列名称
      //cellValue 列的值
      //index 下标
      //alert(cellValue);
      return cellValue == 0 ? "存在" : "消失了";
    },
    //处理每页数量发生变化
    handleSizeChange(val) {
      //console.log(`每页 ${val} 条`);
      //让每页数量发生变化
      this.pageSize = val;
      //重载数据
      this.loadData();
    },
    //处理当前页变化
    handleCurrentChange(val) {
      //console.log(`当前页: ${val}`);
      //让每页数量发生变化
      this.currentPage = val;
      //重载数据
      this.loadData();
    },
    //查询
    search() {
      this.loadData();
    },
    //刷新
    flush() {
      location.reload();
    },
    submitForm1(){
      this.loadData();
      this.dialogFormVisible=false
    },
    //处理逻辑删除
    handleDelete(index, row) {
      //console.log(index, row);
      //确认框提示
      this.$confirm("此操作将永久删除该数据, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          //让_this等于当前vue对象
          var _this = this;
          // alert(111);
          //赋值 $http官网代码  this.form =form:{deptName:'',description:'',status:1}
          _this.$http
            .get("/marketing/api/customerIssues/delete?id=" + row.id) //指定请求的url及参数  get
            .then(function (response) {
              //正确返回对象的处理
              // handle success
              //console.log(response);
              //JSON.stringify   把json转换为json字符串，方便查看
              //console.log(JSON.stringify(response));
              //直接使用this 会指 $http对象  错误
              // this.tableData
              //判断返回值是否正确
              if (response.data.code == 2000) {
                //提示添加成功
                _this.$message({
                  message: "恭喜你，这是一条成功消息",
                  type: "success",
                });
                //重新加载数据
                _this.loadData();
              } else {
                //错误提示信息
                _this.$message.error("错了哦，这是一条错误消息");
              }
            })
            .catch(function (error) {
              //错误的处理
              // handle error
              //console.log(error);
            });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
  },
};
</script>
<style>
.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader .el-upload:hover {
  border-color: #409eff;
}
.avatar-uploader-icon {
  font-size: 178px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
}

.avatar {
  width: 178px;
  height: 178px;
  display: block;
}
</style>
